home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / bfd / syms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  11.9 KB  |  439 lines

  1. /* Generic symbol-table support for the BFD library.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     Symbols
  24.  
  25.     BFD trys to maintain as much symbol information as it can when
  26.     it moves information from file to file. BFD passes information
  27.     to applications though the <<asymbol>> structure. When the
  28.     application requests the symbol table, BFD reads the table in
  29.     the native form and translates parts of it into the internal
  30.     format. To maintain more than the infomation passed to
  31.     applications some targets keep some information 'behind the
  32.     sceans', in a structure only the particular back end knows
  33.     about. For example, the coff back end keeps the original
  34.     symbol table structure as well as the canonical structure when
  35.     a BFD is read in. On output, the coff back end can reconstruct
  36.     the output symbol table so that no information is lost, even
  37.     information unique to coff which BFD doesn't know or
  38.     understand. If a coff symbol table was read, but was written
  39.     through an a.out back end, all the coff specific information
  40.     would be lost. The symbol table of a BFD
  41.     is not necessarily read in until a canonicalize request is
  42.     made. Then the BFD back end fills in a table provided by the
  43.     application with pointers to the canonical information.  To
  44.     output symbols, the application provides BFD with a table of
  45.     pointers to pointers to <<asymbol>>s. This allows applications
  46.     like the linker to output a symbol as read, since the 'behind
  47.     the sceens' information will be still available. 
  48. @menu
  49. @* Reading Symbols::
  50. @* Writing Symbols::
  51. @* typedef asymbol::
  52. @* symbol handling functions::
  53. @end menu
  54.  
  55. INODE
  56. Reading Symbols, Writing Symbols, Symbols, Symbols
  57. SUBSECTION
  58.     Reading Symbols
  59.  
  60.     There are two stages to reading a symbol table from a BFD;
  61.     allocating storage, and the actual reading process. This is an
  62.     excerpt from an appliction which reads the symbol table:
  63.  
  64. |      unsigned int storage_needed;
  65. |      asymbol **symbol_table;
  66. |      unsigned int number_of_symbols;
  67. |      unsigned int i;
  68. |    
  69. |      storage_needed = get_symtab_upper_bound (abfd);
  70. |    
  71. |      if (storage_needed == 0) {
  72. |         return ;
  73. |      }
  74. |      symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
  75. |        ...
  76. |      number_of_symbols = 
  77. |         bfd_canonicalize_symtab (abfd, symbol_table); 
  78. |    
  79. |      for (i = 0; i < number_of_symbols; i++) {
  80. |         process_symbol (symbol_table[i]);
  81. |      }
  82.  
  83.     All storage for the symbols themselves is in an obstack
  84.     connected to the BFD, and is freed when the BFD is closed.
  85.  
  86.  
  87. INODE
  88. Writing Symbols, typedef asymbol, Reading Symbols, Symbols
  89. SUBSECTION
  90.     Writing Symbols
  91.  
  92.     Writing of a symbol table is automatic when a BFD open for
  93.     writing is closed. The application attaches a vector of
  94.     pointers to pointers to symbols to the BFD being written, and
  95.     fills in the symbol count. The close and cleanup code reads
  96.     through the table provided and performs all the necessary
  97.     operations. The outputing code must always be provided with an
  98.     'owned' symbol; one which has come from another BFD, or one
  99.     which has been created using <<bfd_make_empty_symbol>>.   An
  100.     example showing the creation of a symbol table with only one element:
  101.  
  102. |    #include "bfd.h"
  103. |    main() 
  104. |    {
  105. |      bfd *abfd;
  106. |      asymbol *ptrs[2];
  107. |      asymbol *new;
  108. |    
  109. |      abfd = bfd_openw("foo","a.out-sunos-big");
  110. |      bfd_set_format(abfd, bfd_object);
  111. |      new = bfd_make_empty_symbol(abfd);
  112. |      new->name = "dummy_symbol";
  113. |      new->section = bfd_make_section_old_way(abfd, ".text");
  114. |      new->flags = BSF_GLOBAL;
  115. |      new->value = 0x12345;
  116. |    
  117. |      ptrs[0] = new;
  118. |      ptrs[1] = (asymbol *)0;
  119. |      
  120. |      bfd_set_symtab(abfd, ptrs, 1);
  121. |      bfd_close(abfd);
  122. |    }
  123. |    
  124. |    ./makesym 
  125. |    nm foo
  126. |    00012345 A dummy_symbol
  127.  
  128.     Many formats cannot represent arbitary symbol information; for
  129.      instance the <<a.out>> object format does not allow an
  130.     arbitary number of sections. A symbol pointing to a section
  131.     which is not one  of <<.text>>, <<.data>> or <<.bss>> cannot
  132.     be described. 
  133.  
  134. */
  135.  
  136.  
  137. /*
  138. INODE
  139. typedef asymbol, symbol handling functions, Writing Symbols, Symbols
  140.  
  141. */
  142. /*
  143. SUBSECTION
  144.     typedef asymbol
  145.  
  146.     An <<asymbol>> has the form:
  147.  
  148. */
  149.  
  150. /*
  151. CODE_FRAGMENT
  152.  
  153. .typedef struct symbol_cache_entry 
  154. .{
  155. .    {* A pointer to the BFD which owns the symbol. This information
  156. .       is necessary so that a back end can work out what additional
  157. .          (invisible to the application writer) information is carried
  158. .       with the symbol.  *}
  159. .
  160. .  struct _bfd *the_bfd;
  161. .
  162. .    {* The text of the symbol. The name is left alone, and not copied - the
  163. .       application may not alter it. *}
  164. .  CONST char *name;
  165. .
  166. .    {* The value of the symbol.*}
  167. .  symvalue value;
  168. .
  169. .    {* Attributes of a symbol: *}
  170. .
  171. .#define BSF_NO_FLAGS    0x00
  172. .
  173. .    {* The symbol has local scope; <<static>> in <<C>>. The value
  174. .        is the offset into the section of the data. *}
  175. .#define BSF_LOCAL    0x01
  176. .
  177. .    {* The symbol has global scope; initialized data in <<C>>. The
  178. .       value is the offset into the section of the data. *}
  179. .#define BSF_GLOBAL    0x02
  180. .
  181. .    {* Obsolete *}
  182. .#define BSF_IMPORT    0x04
  183. .
  184. .    {* The symbol has global scope, and is exported. The value is
  185. .       the offset into the section of the data. *}
  186. .#define BSF_EXPORT    0x08
  187. .
  188. .    {* The symbol is undefined. <<extern>> in <<C>>. The value has
  189. .       no meaning. *}
  190. .#define BSF_UNDEFINED_OBS 0x10    
  191. .
  192. .    {* The symbol is common, initialized to zero; default in
  193. .       <<C>>. The value is the size of the object in bytes. *}
  194. .#define BSF_FORT_COMM_OBS    0x20    
  195. .
  196. .    {* A normal C symbol would be one of:
  197. .       <<BSF_LOCAL>>, <<BSF_FORT_COMM>>,  <<BSF_UNDEFINED>> or
  198. .       <<BSF_EXPORT|BSD_GLOBAL>> *}
  199. .
  200. .    {* The symbol is a debugging record. The value has an arbitary
  201. .       meaning. *}
  202. .#define BSF_DEBUGGING    0x40
  203. .
  204. .    {* Used by the linker *}
  205. .#define BSF_KEEP        0x10000
  206. .#define BSF_KEEP_G      0x80000
  207. .
  208. .    {* Unused *}
  209. .#define BSF_WEAK        0x100000
  210. .#define BSF_CTOR        0x200000 
  211. .
  212. .       {* This symbol was created to point to a section *}
  213. .#define BSF_SECTION_SYM 0x400000 
  214. .
  215. .    {* The symbol used to be a common symbol, but now it is
  216. .       allocated. *}
  217. .#define BSF_OLD_COMMON  0x800000  
  218. .
  219. .    {* The default value for common data. *}
  220. .#define BFD_FORT_COMM_DEFAULT_VALUE 0
  221. .
  222. .    {* In some files the type of a symbol sometimes alters its
  223. .       location in an output file - ie in coff a <<ISFCN>> symbol
  224. .       which is also <<C_EXT>> symbol appears where it was
  225. .       declared and not at the end of a section.  This bit is set
  226. .         by the target BFD part to convey this information. *}
  227. .
  228. .#define BSF_NOT_AT_END    0x40000
  229. .
  230. .    {* Signal that the symbol is the label of constructor section. *}
  231. .#define BSF_CONSTRUCTOR   0x1000000
  232. .
  233. .    {* Signal that the symbol is a warning symbol. If the symbol
  234. .       is a warning symbol, then the value field (I know this is
  235. .       tacky) will point to the asymbol which when referenced will
  236. .       cause the warning. *}
  237. .#define BSF_WARNING       0x2000000
  238. .
  239. .    {* Signal that the symbol is indirect. The value of the symbol
  240. .       is a pointer to an undefined asymbol which contains the
  241. .       name to use instead. *}
  242. .#define BSF_INDIRECT     0x4000000
  243. .
  244. .  flagword flags;
  245. .
  246. .    {* A pointer to the section to which this symbol is 
  247. .       relative.  This will always be non NULL, there are special
  248. .          sections for undefined and absolute symbols *}
  249. .  struct sec *section;
  250. .
  251. .    {* Back end special data. This is being phased out in favour
  252. .       of making this a union. *}
  253. .  PTR udata;    
  254. .
  255. .} asymbol;
  256. */
  257.  
  258. #include "bfd.h"
  259. #include "sysdep.h"
  260. #include "libbfd.h"
  261. #include "aout/stab_gnu.h"
  262.  
  263. /*
  264. INODE
  265. symbol handling functions,  , typedef asymbol, Symbols
  266. SUBSECTION
  267.     Symbol Handling Functions
  268. */
  269.  
  270. /*
  271. FUNCTION
  272.     get_symtab_upper_bound
  273.  
  274. DESCRIPTION
  275.     Returns the number of bytes required in a vector of pointers
  276.     to <<asymbols>> for all the symbols in the supplied BFD,
  277.     including a terminal NULL pointer. If there are no symbols in
  278.     the BFD, then 0 is returned.
  279.  
  280. .#define get_symtab_upper_bound(abfd) \
  281. .     BFD_SEND (abfd, _get_symtab_upper_bound, (abfd))
  282.  
  283. */
  284.  
  285. /*
  286. FUNCTION
  287.     bfd_canonicalize_symtab
  288.  
  289. DESCRIPTION
  290.     Supplied a BFD and a pointer to an uninitialized vector of
  291.     pointers. This reads in the symbols from the BFD, and fills in
  292.     the table with pointers to the symbols, and a trailing NULL.
  293.     The routine returns the actual number of symbol pointers not
  294.     including the NULL.
  295.  
  296.  
  297. .#define bfd_canonicalize_symtab(abfd, location) \
  298. .     BFD_SEND (abfd, _bfd_canonicalize_symtab,\
  299. .                  (abfd, location))
  300.  
  301. */
  302.  
  303.  
  304. /*
  305. FUNCTION
  306.     bfd_set_symtab
  307.  
  308. DESCRIPTION
  309.     Provided a table of pointers to symbols and a count, writes to
  310.     the output BFD the symbols when closed.
  311.  
  312. SYNOPSIS
  313.     boolean bfd_set_symtab (bfd *, asymbol **, unsigned int );
  314. */
  315.  
  316. boolean
  317. bfd_set_symtab (abfd, location, symcount)
  318.      bfd *abfd;
  319.      asymbol **location;
  320.      unsigned int symcount;
  321. {
  322.   if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
  323.     bfd_error = invalid_operation;
  324.     return false;
  325.   }
  326.  
  327.   bfd_get_outsymbols (abfd) = location;
  328.   bfd_get_symcount (abfd) = symcount;
  329.   return true;
  330. }
  331.  
  332. /*
  333. FUNCTION
  334.     bfd_print_symbol_vandf
  335.  
  336. DESCRIPTION
  337.     Prints the value and flags of the symbol supplied to the stream file.
  338.  
  339. SYNOPSIS
  340.     void bfd_print_symbol_vandf(PTR file, asymbol *symbol);
  341. */
  342. void
  343. DEFUN(bfd_print_symbol_vandf,(file, symbol),
  344. PTR file AND
  345. asymbol *symbol)
  346. {
  347.   flagword type = symbol->flags;
  348.   if (symbol->section != (asection *)NULL)
  349.       {
  350.     fprintf_vma(file, symbol->value+symbol->section->vma);
  351.       }
  352.   else 
  353.       {
  354.     fprintf_vma(file, symbol->value);
  355.       }
  356.   fprintf(file," %c%c%c%c%c%c%c%c",
  357.       (type & BSF_LOCAL)  ? 'l':' ',
  358.       (type & BSF_GLOBAL) ? 'g' : ' ',
  359.       (type & BSF_IMPORT) ? 'i' : ' ',
  360.       (type & BSF_EXPORT) ? 'e' : ' ',
  361.       (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
  362.       (type & BSF_WARNING) ? 'W' : ' ',
  363.       (type & BSF_INDIRECT) ? 'I' : ' ',
  364.       (type & BSF_DEBUGGING) ? 'd' :' ');
  365.  
  366. }
  367.  
  368.  
  369. /*
  370. FUNCTION
  371.     bfd_make_empty_symbol
  372.  
  373. DESCRIPTION
  374.     This function creates a new <<asymbol>> structure for the BFD,
  375.     and returns a pointer to it.
  376.  
  377.     This routine is necessary, since each back end has private
  378.     information surrounding the <<asymbol>>. Building your own
  379.     <<asymbol>> and pointing to it will not create the private
  380.     information, and will cause problems later on.
  381.  
  382. .#define bfd_make_empty_symbol(abfd) \
  383. .     BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
  384. */
  385.  
  386. /*
  387. FUNCTION
  388.     bfd_decode_symclass
  389.  
  390. DESCRIPTION
  391.     Return a lower-case character corresponding to the symbol
  392.     class of symbol.
  393.  
  394. SYNOPSIS
  395.     int bfd_decode_symclass(asymbol *symbol);
  396. */
  397. int
  398. DEFUN(bfd_decode_symclass,(symbol),
  399. asymbol *symbol)
  400. {
  401.   flagword flags = symbol->flags;
  402.   
  403.   if (symbol->section == &bfd_com_section) return 'C';
  404.   if (symbol->section == &bfd_und_section) return 'U';
  405.  
  406.    if ( flags & (BSF_GLOBAL|BSF_LOCAL) ) {
  407.      if ( symbol->section == &bfd_abs_section)
  408.       return (flags & BSF_GLOBAL) ? 'A' : 'a';
  409.      else if ( !strcmp(symbol->section->name, ".text") )
  410.        return (flags & BSF_GLOBAL) ? 'T' : 't';
  411.      else if ( !strcmp(symbol->section->name, ".data") )
  412.        return (flags & BSF_GLOBAL) ? 'D' : 'd';
  413.      else if ( !strcmp(symbol->section->name, ".bss") )
  414.        return (flags & BSF_GLOBAL) ? 'B' : 'b';
  415.      else
  416.        return (flags & BSF_GLOBAL) ? 'O' : 'o';
  417.     }
  418.  
  419.   /* We don't have to handle these cases just yet, but we will soon:
  420.      N_SETV: 'v'; 
  421.      N_SETA: 'l'; 
  422.      N_SETT: 'x';
  423.      N_SETD: 'z';
  424.      N_SETB: 's';
  425.      N_INDR: 'i';
  426.      */
  427.  
  428.   return '?';
  429. }
  430.  
  431.  
  432. void
  433. bfd_symbol_is_absolute()
  434. {
  435.   
  436.   abort();
  437. }
  438.  
  439.